*Set directory*
cd "[INSERT WORKING DIRECTORY HERE]"

*Import clean stata data* 
*this dataset does not have the comment variables, and the "Duration (in seconds) is renamed "Duration_in_seconds"*
use "clean_conjoint_values.dta", clear

******Conjoint Analysis******
*****************************	
reg choice female_vol age_twentytwo_vol age_fortyseven_vol race_africanamerican_vol race_hispanic_vol ded_cons_vol ded_incon_vol comp_con_vol comp_incon_vol i.id_vol_scaled, cluster(RecipientEmail)
estimates store m1

reg choice female_vol age_twentytwo_vol age_fortyseven_vol race_africanamerican_vol race_hispanic_vol ded_cons_vol ded_incon_vol comp_con_vol comp_incon_vol i.id_vol_scaled##c.CandidateIdeology_scaled, cluster(RecipientEmail)
estimates store m2

reg choice female_vol age_twentytwo_vol age_fortyseven_vol race_africanamerican_vol race_hispanic_vol ded_cons_vol ded_incon_vol comp_con_vol comp_incon_vol i.id_vol_scaled##c.PersonalIdeology_scaled, cluster(RecipientEmail)
estimates store m3

**Baseline attribute levels: gender=male, age=72, race=hispanic/latino, dedication=no information, reliability=no information ideology=moderate


******Margingsplot***********
*****************************	

*Create marginsplot for ACME plot that describes the interaction between the conjoint volunteer's ideology and the ideology of the respondent's candidate

*Original Conjoint regression
estimates restore m2

**Generating predicted probabilities 
*Compute the predictive margins for every value from 1 to 3 of CandidateIdeology_scaled for each level of id_vol_scaled. The predictive margins for this model are the linear predictions of selecting the volunteer for the three values of CandidateIdeology_scaled for each level of id_vol_scaled.
*https://stats.idre.ucla.edu/stata/faq/how-can-i-use-the-margins-command-to-understand-multiple-interactions-in-regression-and-anova-stata-11/
margins id_vol_scaled, at(CandidateIdeology_scaled=(1(1)3)) post

**Ploting Predicted probabilities**
set scheme plotplainblind
marginsplot, recastci(rcap) yline(.50) title("Likelihood of Selecting Volunteer") xtitle("Candidate Ideological Extremity") ytitle("") xlabel(1 "Moderate" 2 "Conservative/Liberal" 3 "Very Conservative/Liberal", angle(25)) ytitle("Predicted Likelihood") legend(order(1 "Moderate Volunteer" 2 "Very Conservative/Progressive Volunteer") position(6) ring(0))
graph export doc\figs\interaction_candidateideology.tif, replace



*Create marginsplot for ACME plot that describes the interaction between the conjoint volunteer's ideology and the ideology of the respondent

*Original Conjoint regression
estimates restore m3

**Generating predicted probabilities
*Compute the predictive margins for every value from 1 to 3 of PersonalIdeology_scaled for each level of id_vol_scaled. The predictive margins for this model are the linear predictions of selecting the volunteer for the three values of PersonalIdeology_scaled for each level of id_vol_scaled.

*https://stats.idre.ucla.edu/stata/faq/how-can-i-use-the-margins-command-to-understand-multiple-interactions-in-regression-and-anova-stata-11/
margins id_vol_scaled, at(PersonalIdeology_scaled=(1(1)3)) post

**Ploting Predicted probabilities**
set scheme plotplainblind
marginsplot, recastci(rcap) yline(.50) title("Likelihood of Selecting Volunteer") xtitle("Respondent Ideological Extremity") ytitle("") xlabel(1 "Moderate" 2 "Conservative/Liberal" 3 "Very Conservative/Liberal", angle(25)) ytitle("Predicted Likelihood") legend(order(1 "Moderate Volunteer" 2 "Very Conservative/Progressive Volunteer") position(6) ring(0))
graph export doc\figs\interaction_personalideology.tif, replace




*From Hans (email) 5.8.2021

**Original conjoint regression**
*reg moreelectable i.ConjCandRace##c.minority i.ConjFemale##c.rfemale ib2.ConjAgeGroup ConjIdeologyExt##RIdeolExt i.ConjCandQuality i.ConjCharacterRank i.ConjStumpSpeech, cluster (paircomparison)

**Generating predicted probabilities for moderate respondents and extremist respondents (leaving out leaners and weak partisans (RIdeol=2 and 3))**
*margins ConjIdeologyExt#RIdeolExt if RIdeolExt!=2 & RIdeolExt!=3, asbalanced level(95) post

**Ploting Predicted probabilities**
*set scheme plotplainblind
*marginsplot, recastci(rcap) yline(.50) title("Likelihood of Selecting Candidate as More Electable") xtitle("Candidate Ideological Extremity") ytitle("") xlabel(1 "Moderate" 2 "Conservative/Liberal" 3 "Very Conservative/Liberal", angle(25)) yscale(range(.35(.1).65)) ylabel(.35(.1).65) ytitle("Predicted Likelihood") legend(order(1 "Moderate Respondent" 2 "Very Conservative/Very Liberal Respondent") position(6) ring(0))